package com.hgk.common.common.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import com.common.android.volley.VolleyError;
import com.common.android.volley.toolbox.RequestListener;
import com.common.android.volley.toolbox.VolleyManager;
import com.common.dongqiuhui.apps.comm.model.BaseController;
import com.common.dongqiuhui.apps.comm.tools.BallLog;
import com.hgk.common.common.ui.view.RippleView;
import com.hgk.common.common.ui.view.TipView;

import java.util.Map;

import width.hgk.wanwan.R;

/**
 * 普通页面的联网请求类
 * Created by sam on 15/7/15.
 */
public abstract class SimpleBaseActivity extends BallPhotoToolActivity {

    public boolean mIsNeedNet=true;//是否需要判断有无网络，默认需要
    protected TipView mTipView;
    protected  boolean isShowAnim;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 加载数据
     */
    protected void getNetDataForPost(final String action,String url,Class classOfT){
        if(mTipView!=null&&isShowAnim){
            mTipView.setVisibility(View.VISIBLE);
            mTipView.startAnim();
        }
        VolleyManager.getInstance().postString(url, getApiParams(action), new RequestListener() {
            @Override
            public void requestSuccess(Object result) {
                if (mTipView != null&&isShowAnim) {
                    mTipView.stopAnim();
                    mTipView.setVisibility(View.GONE);
                }
                BaseController baseController;
                if (result instanceof BaseController) {
                    baseController = (BaseController) result;
                    BallLog.i("fyh", "1231313baseController code=" + baseController.getCode());
                    if ("10000".equals(baseController.getCode())) {
                        success(action, result);
                    } else {
                        BallLog.i("fyh", "12312313123 code=" + baseController.getMsg());
                        fail(action, baseController.getMsg());
                    }
                }
            }

            @Override
            public void requestError(VolleyError e) {
                if (e.isNonetResponse()&&isShowAnim) {
                    noNetView();
                }
                netfail(action, e);
            }
        }, classOfT);
    }

    protected void getNetDateForGet(final String action,String url,Class classOfT){
        VolleyManager.getInstance().get(url, getApiParams(action), new RequestListener() {
            @Override
            public void requestSuccess(Object result) {
                BaseController baseController;
                if (result instanceof BaseController) {
                    baseController = (BaseController) result;
                    if ("10000".equals(baseController.getCode())) {
                        success(action, result);
                    } else {
                        fail(action, baseController.getMsg());
                    }
                }
            }

            @Override
            public void requestError(VolleyError e) {
                fail(action, e.getMessage());
            }
        }, classOfT);
    }
    /**
     * Api参数
     */
    protected abstract Map getApiParams(String action);

    /**
     * 成功
     */
    protected  abstract void success(String action,Object result);

    protected  abstract void fail(String action,String errorStr);

    protected  abstract void netfail(String action,VolleyError volleyError);

    @Override
    protected void getBitmap(Bitmap bitmap, String path) {

    }

    public void noNetView(){
        if (mTipView!=null){
            mTipView.getmIvLoading().setVisibility(View.GONE);
            mTipView.getmTvLoading().setVisibility(View.GONE);
            mTipView.stopAnim();
            mTipView.getmRvNoNet().setVisibility(View.VISIBLE);
            mTipView.getmIvNoNet().setVisibility(View.VISIBLE);
            mTipView.getmTvNoNet().setVisibility(View.VISIBLE);
            mTipView.getmIvNoNet().setBackgroundResource(R.drawable.nonet_icon);
            mTipView.getmRvNoNet().setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    noNetReflushBtn();
                }
            });
            mTipView.setVisibility(View.VISIBLE);
        }
    }
    public void ShowNoDataView(String tipStr){
        if (mTipView!=null){
            mTipView.getmIvLoading().setVisibility(View.GONE);
            mTipView.getmTvLoading().setVisibility(View.GONE);
            mTipView.stopAnim();
            mTipView.getmIvNoNet().setBackgroundResource(R.drawable.nodata_icon);
            mTipView.getmRvNoNet().setVisibility(View.GONE);
            mTipView.getmIvNoNet().setVisibility(View.VISIBLE);
            mTipView.getmTvNoNet().setVisibility(View.VISIBLE);
            mTipView.getmTvNoNet().setText(tipStr);
            mTipView.setVisibility(View.VISIBLE);
        }
    }
    public void showLoadingView(){
        if (mTipView!=null){
            mTipView.getmIvLoading().setVisibility(View.VISIBLE);
            mTipView.startAnim();
            mTipView.getmTvLoading().setText("");
            mTipView.getmRvNoNet().setVisibility(View.GONE);
            mTipView.getmIvNoNet().setVisibility(View.GONE);
            mTipView.getmTvNoNet().setVisibility(View.GONE);
            mTipView.setVisibility(View.VISIBLE);
        }
    }


}
